//
//  DHS2_SearchView_News.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/15.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_SearchView_News.h"
#import "DHMagazine_LargeCell.h"
#import "DHMagazine_NormalCell.h"
#import "DHNewsDetailController.h"

@interface DHS2_SearchView_News()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (assign, nonatomic) NSInteger pageIndex;
@property (strong, nonatomic) NSMutableArray *newsArr;

@end

@implementation DHS2_SearchView_News

+ (DHS2_SearchView_News *)shareDHS2_SearchView_News{
    DHS2_SearchView_News *view = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([DHS2_SearchView_News class]) owner:self options:nil] objectAtIndex:0];
    return view;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHMagazine_LargeCell" bundle:nil] forCellReuseIdentifier:kDHMagazine_LargeCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHMagazine_NormalCell" bundle:nil] forCellReuseIdentifier:kDHMagazine_NormalCell];
    self.tableView.mj_header = [DHMJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(searchData)];
    self.tableView.mj_footer = [DHMJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(searchMoreData)];
}

- (NSMutableArray *)newsArr{
    if (!_newsArr) {
        _newsArr = [NSMutableArray array];
    }
    return _newsArr;
}

//搜索
- (void)searchData{
    self.pageIndex = 0;
    [self.tableView.mj_footer resetNoMoreData];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:@"资讯" forKey:@"type"];
    [params setObject:self.keyWord forKey:@"key"];
    [params setObject:@(self.pageIndex) forKey:@"pageIndex"];
    [params setObject:@(20) forKey:@"pageSize"];
    [[DHNetAPIClient shareDHNetAPIClient] request_app_searchWithParams:params AndBlock:^(id data, NSError *error) {
        if (data) {
            [self.tableView.mj_header endRefreshing];
            
            [self.newsArr removeAllObjects];
            
            NSDictionary *dic = [data objectForKey:@"results"];
            [self.newsArr addObjectsFromArray:[dic objectForKey:@"articles"]];
            
            if (self.newsArr.count == 0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            
            [self.tableView reloadData];
        }
    }];
}

- (void)searchMoreData{
    [self.tableView.mj_footer endRefreshing];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.newsArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.newsArr objectAtIndex:indexPath.row];
    if ([[model objectForKey:@"layout"] isEqualToString:@"大矩形"]) {
        return 210;
    }else{
        return 100;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = self.newsArr[indexPath.row];
    if ([[model objectForKey:@"layout"] isEqualToString:@"大矩形"]) {
        DHMagazine_LargeCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHMagazine_LargeCell forIndexPath:indexPath];
        [cell updateCell:model];
        return cell;
    }else{
        DHMagazine_NormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHMagazine_NormalCell forIndexPath:indexPath];
        [cell updateCell:model];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.newsArr objectAtIndex:indexPath.row];
    DHNewsDetailController *vc = [[DHNewsDetailController alloc] initWithArticleID:[model objectForKey:@"_id"]];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

@end
